$(function() {
    var a = {
            info: "http://preorder.oneplus.net/ajax/info?" + window.location.href.split("?")[1],
            login: "http://preorder.oneplus.net/login"
        },
        b = {
            flagRoot: "http://a.onepluscontent.com/skin/frontend/oneplus2014/default/images/flags/",
            nextPage: "http://preorder.oneplus.net/payment/paypal?" + window.location.href.split("?")[1],
            data: {}
        },
        c = function() {
            this.initialize(), this.$pay.on("click", function() {
                var c = $(this);
                c.attr("disabled", !0), setTimeout(function() {
                    c.attr("disabled", !1)
                }, 2e3);
                var d = cookieTools.getItem("userId");
                if (d && "0" != d) {
                    window.location.href = b.nextPage, ga("create", "UA-46480385-11", "auto"), ga("require", "ec");
                    for (var e = b.data.products, f = 0; e > f; f++) {
                        var g = e[f];
                        ga("ec:addProduct", {
                            id: g.sku,
                            name: g.name,
                            brand: "OnePlus",
                            price: (g.price/1.24108),
                            quantity: g.num - 0
                        })
                    }
                    ga("ec:setAction", "checkout", {
                        step: 3,
                        id: response.data.orderSn || d,
                        affiliation: "OnePlus One pre-order",
                        revenue: (b.data.totalFee - 0).toFixed(2),
                        shipping: (b.data.shipFee - 0).toFixed(2)
                    }), ga("send", "pageview")
                } else window.location.href = a.login;
                return !1
            })
        };
    c.prototype = {
        $time: $("#estimateTime"),
        $summary: $("#summary"),
        $countDown: $("#countDown"),
        twoMins: 12e4,
        $pay: $("#pay"),
        initialize: function() {
            var c = this;
            $.ajax({
                url: a.info,
                data: {
                    format: "json"
                },
                type: "post",
                dataType: "json",
                success: function(a) {
                    $(".loading-mask").remove(), 0 === a.ret && ($(".on-loading").css({
                        visibility: "hidden"
                    }), c.$time.text(a.data.ship_plan), c.$countDown.length > 0 && c.refreshTime(), c.renderUserBill(a.data), b.data = a.data)
                },
                timeout: 3e4,
                error: function(a, b) {
                    $(".loading-mask").remove(), "timeout" == b ? $("#timeoutInfo").modal("show") : $("#errorInfo").modal("show")
                }
            })
        },
        refreshTime: function() {
            var b = this,
                c = Date.now(),
                d = new Date(c + this.twoMins);
            b.timer && clearInterval(b.timer), b.timer = setInterval(function() {
                var c = Date.now(),
                    e = d - c,
                    f = 1e3,
                    g = 60 * f;
                if (0 >= e) $.ajax({
                    url: a.info,
                    data: {
                        format: "json"
                    },
                    type: "post",
                    dataType: "json",
                    success: function(a) {
                        0 === a.ret && (b.$time.text(a.data.ship_plan), b.refreshTime())
                    }
                });
                else {
                    var h = ("00" + Math.floor(e / g)).slice(-2),
                        i = ("00" + Math.floor(e % g / f)).slice(-2);
                    b.$countDown.text(h + ":" + i)
                }
            }, 300)
        },
        renderUserBill: function(a) {
            var c = this.$summary.find(".js-country-img img");
            c[0].src = b.flagRoot + a.isoCode.toLowerCase() + ".png", c[0].alt = a.isoCode;
            var d = a.products,
                e = "";
            this.$summary.find("tr.js-bill-item").remove();
            for (var f = 0; f < d.length; f++) {
                var g = d[f];
                e += '<tr class="js-bill-item"><td class="col-md-2"><img src="' + g.img + '"width="94"height="94"alt=""></td><td class="col-md-8"><span class="js-name">' + g.name + '</span></td><td class="col-md-1">x<span class="js-count">' + g.num + '</span></td><td class="text-right col-md-1"><span class="js-unit">$</span><span class="js-price">' + g.price + "</span></td></tr>"
            }
            $(e).insertBefore(this.$summary.find(".js-divider"));
            var h = this.$summary.find(".js-ship-method");
            h.find(".js-name").text(a.ship_name), h.find(".js-price").text((a.shipFee - 0).toFixed(2));
            var i = this.$summary.find(".js-subtotal");
            i.find(".js-price").text((a.totalFee - 0).toFixed(2));
            var j = this.$summary.find(".js-ship-address"),
                k = a.addr.ship_addr;
            j.find(".js-first-name").text(k.firstName), j.find(".js-last-name").text(k.lastName), j.find(".js-company").text(k.company), j.find(".js-address-one").text(k.street1), j.find(".js-address-two").text(k.street2), j.find(".js-city").text(k.city), j.find(".js-state").text(k.stateName), j.find(".js-zip").text(k.zip), j.find(".js-country").text(k.countryName);
            var l = this.$summary.find(".js-bill-address"),
                m = a.addr.bill_addr;
            l.find(".js-first-name").text(m.firstName), l.find(".js-last-name").text(m.lastName), l.find(".js-company").text(m.company), l.find(".js-address-one").text(m.street1), l.find(".js-address-two").text(m.street2), l.find(".js-city").text(m.city), l.find(".js-state").text(m.stateName), l.find(".js-zip").text(m.zip), l.find(".js-country").text(m.countryName);
            var n = {
                GBP: "￡",
                USD: "$",
                SEK: "SEK",
                DKK: "DKK",
                HKD: "HKD",
                TWD: "TWD",
                EUR: "&#x20AC;"
            };
            $(".js-unit").html(n[a.currency] || "$")
        }
    }, new c
});